--
-- Author: shunguo.chen
-- Date: 2017-06-21 16:49:56
--
-- IMPriRender

local AvatarIconNode = require("app.widget.AvatarIconNode")

local ClassRef = app.mvc.createRenderClass(...)

-- data = attr
function ClassRef.create(node)
	local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.IMTargetNode))
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init()
	return sprite
end

function ClassRef:init(data)
	self.super.init(self, data)

	-- 是否有选中状态
	self._hasSelected = true
	
	self:setContentSize(self._ui.Panel:getContentSize())
	self:setNum(0)
	self:setSelected(false)

	self._avatarIcon = AvatarIconNode.new(self._ui.Panel, "icon")

	display.uiAddClick(self._ui.Panel, function()
			if self._hasSelected then
				if not self:isSelected() then
					self:setSelected(true)
					executeFunc(self._onClick, self)
				end
			else
				executeFunc(self._onClick, self)
			end
		end)

	self:refresh()
end

function ClassRef:refresh()
	if self._data then
		self._avatarIcon:update(self._data)
	else
		self._ui.icon:loadTexture(Res.IMSystemIcon, 1)
	end
end

function ClassRef:setNum(num)
	self._num = num
	self._ui.bg_num:setVisible(num > 0)
	if num > 0 then
		self._ui.Text_num:setString(num)
	end
end

function ClassRef:addNum(num)
	-- print("IMPriRender.addNum", self._num + 1)
	num = num or 1
	self:setNum(self._num + num)
end

function ClassRef:setHasSelected(b)
	self._hasSelected = b
	if not b then
		self._ui.mask:setVisible(false)
	end
end

function ClassRef:setSelected(b)
	self._ui.mask:setVisible(not b)
end
function ClassRef:isSelected()
	return not self._ui.mask:isVisible()
end

function ClassRef:onClick(cb)
	self._onClick = cb
end

return ClassRef